// CMouseRecorder.cpp: implementation of the CMouseRecorder class.
//
//////////////////////////////////////////////////////////////////////

#include "StdAfx.h"
#include "Common.h"
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <strsafe.h>
#include <string.h>

extern HANDLE g_hModule;

char* GetCurrDir()
{
	static char defaultFileName[MAX_PATH+1] = "\0";
	if(defaultFileName[0] == '\0')
	{
		GetCurrentDirectory(MAX_PATH, defaultFileName);
	}
	return defaultFileName;
}

char* GetOutputDir()
{
	static char defaultFileName[MAX_PATH+1] = "\0";
	if(defaultFileName[0] == '\0')
	{
		GetCurrentDirectory(MAX_PATH, defaultFileName);

		char strtime[128];
		SYSTEMTIME localtime;
		GetLocalTime(&localtime);
		if(localtime.wHour < 9)
		{
			localtime.wDay--;
		}
		sprintf(strtime, "\\T0002\\export\\%d%02d%02d\\", localtime.wYear, localtime.wMonth, localtime.wDay);
		strcat(defaultFileName, strtime);
	}
	return defaultFileName;
}

char* GetOutputFileName()
{
	static char defaultFileName[MAX_PATH+1] = "\0";
	if(defaultFileName[0] == '\0')
	{
		strcpy(defaultFileName, GetOutputDir());

		char strtime[128];
		SYSTEMTIME localtime;
		GetLocalTime(&localtime);
		sprintf(strtime, "cskb%d%02d%02d%02d%02d.xls", localtime.wYear, localtime.wMonth, localtime.wDay, localtime.wHour, localtime.wMinute);
		strcat(defaultFileName, strtime);
	}
	return defaultFileName;
}

char* GetLogFileName()
{
	static char defaultFileName[MAX_PATH+1] = "\0";
	if(defaultFileName[0] == '\0')
	{
		DWORD pathLength = ::GetModuleFileName((HMODULE)g_hModule, defaultFileName, MAX_PATH);
		defaultFileName[pathLength-1] = 'g';
		defaultFileName[pathLength-2] = 'o';
		defaultFileName[pathLength-3] = 'l';
	}
	//MessageBox(NULL, defaultFileName, NULL, MB_OK);
	return defaultFileName;
}

char* GetExeName()
{
	static char defaultFileName[MAX_PATH+1] = "\0";
	if(defaultFileName[0] == '\0')
	{
		DWORD pathLength = ::GetModuleFileName((HMODULE)g_hModule, defaultFileName, MAX_PATH);
		defaultFileName[pathLength-1] = 'e';
		defaultFileName[pathLength-2] = 'x';
		defaultFileName[pathLength-3] = 'e';
	}
	return defaultFileName;
}


bool   CheckFolderExist( char *strPath)   
{   
    WIN32_FIND_DATA  wfd;   
    bool  rValue  =   false;
	char szPath[MAX_PATH+1];
	strcpy(szPath, strPath);
	if(szPath[strlen(strPath)-1] == '\\')
	{
		szPath[strlen(strPath)-1] ='\0';
	}

    HANDLE hFind  =  FindFirstFile(szPath,  &wfd);   
    if  ((hFind  !=  INVALID_HANDLE_VALUE)  &&  (wfd.dwFileAttributes  & FILE_ATTRIBUTE_DIRECTORY))   
    {   
        rValue  =   true ;      
    }   
    FindClose(hFind);   
    return  rValue;   
} 

BOOL FindFileName(LPCTSTR strFolder, LPTSTR outFileName)
{    
	WIN32_FIND_DATA data;    
	HANDLE hfind;    
	TCHAR nameRule[MAX_PATH+1];
	_tcscpy(nameRule, strFolder);
	_tcscat(nameRule, _T("*output.xls"));    

	hfind = FindFirstFile(nameRule, &data); 
	FILETIME ft = {0, 0};
	while( hfind != INVALID_HANDLE_VALUE )     
	{   
		_tprintf(_T("File: %s\r\n"), data.cFileName);
		if(data.ftLastWriteTime.dwLowDateTime > ft.dwLowDateTime)
		{
			ft.dwLowDateTime = data.ftLastWriteTime.dwLowDateTime;
			_tcscpy(outFileName, data.cFileName);
			size_t namelen = _tcslen(data.cFileName);        
			outFileName[namelen] = '\0'; 
			_tprintf(_T("Find file: %s\r\n"), outFileName);
		}    
		if(  !FindNextFile(hfind,   &data)   )         
		{             
			hfind = INVALID_HANDLE_VALUE;         
		}     
	} 
	
	CloseHandle(hfind);    
	
	return ft.dwLowDateTime > 0;
}